<?php

    namespace Logistics\Controller;

    use Common\Controller\AdminbaseController;

    class AdminBillController extends AdminbaseController {

        protected $m_logistics_bill,$m_card,$m_logistics;

        function _initialize() {
            parent::_initialize();
            $this->m_logistics_bill= D("Logistics/LogisticsBill");
            $this->m_card = D("Admin/Card");
            $this->m_logistics = D("Admin/Logistics");
        }

        /**
         *会员卡结账实时统计
         */
        public  function operation_daily(){
            $card_id_list =get_card_id_by_logistics();

            $start_time = strtotime(date("Y-m-d 00:00:00"));
            $end_time = time();
            $where = array(
              'c.cid'=>array("in",$card_id_list),
               'c.create_time'=>array('between',array($start_time,$end_time))
            );
            $list = M("CardSale")->field("c.*,s.sale_name")->alias("c")->join("bdt_sale s ON s.id = c.sale_id")
                ->where($where)
                ->order("c.sale_id")->select();
            $result = array();
            foreach($list AS $litem){
                $key = date("Ymd",$litem['create_time']);
                if(empty($result[$key])){
                    $result[$key]['time'] = date("Y-m-d");
                    $result[$key]['sale_name'] =$litem['sale_name'];
                    $result[$key]['sale_num'] = 1;
                    $result[$key]['card_num'][] = $litem['cid'];
                    $result[$key]['price'] = $litem['price'];
                    $result[$key]['point'] = $litem['general_point']+$litem['self_point'];
                }else{
                    $result[$key]['sale_num']++;
                    if(!in_array($litem['cid'],$result[$key]['card_num'])){
                        $result[$key]['card_num'][] = $litem['cid'];
                    }
                    $result[$key]['price'] = $result[$key]['price']+$litem['price'];
                    $result[$key]['point'] = $result[$key]['point']+ $litem['general_point']+$litem['self_point'];
                }
            }
            $this->assign("list",$result);
            $this->display();
        }

        /**
         * 会员卡充值实时统计
         */
        public function recharge_daily(){
            $card_id_list =get_card_id_by_logistics();
            $start_time = strtotime(date("Y-m-d 00:00:00"));

            $end_time = time();
            $time = array($start_time,$end_time);
            $where = array(
                "r.model"=>2,
                'r.rid'=>array("IN",$card_id_list),
                'r.create_time'=>array("BETWEEN",$time),
            );
            $list =  M("Recharge")->alias('r')
                ->where($where)
                ->join('bdt_logistics l ON l.id = r.lid',"LEFT")
                ->field("r.*,l.logistics_name")
                ->select();
            $result = array();
            foreach($list AS $litem){
                $key = date("Ymd",$litem['create_time']);
                if(empty($result[$key])){
                    $result[$key]['time'] = date("Y-m-d",$litem['create_time']);
                    $result[$key]['logistics_name'] =$litem['logistics_name'];
                    $result[$key]['price'] = $litem['price'];
                    $result[$key]['give_price'] = $litem['general_price']+$litem['self_price'];
                    $result[$key]['sale_num'] = 1;
                    $result[$key]['card_num'][] = $litem['rid'];

                }else{
                    $result[$key]['sale_num']++;
                    if(!in_array($litem['rid'],$result[$key]['card_num'])){
                        $result[$key]['card_num'][] = $litem['rid'];
                    }
                    $result[$key]['price'] = $result[$key]['price']+$litem['price'];
                    $result[$key]['give_price'] = $result[$key]['give_price']+ $litem['general_price']+$litem['self_price'];
                }
            }
            $this->assign("list",$result);
            $this->display();
        }

        /**
         * 机构实时余额
         */
        public function balance(){
            $list = array();
            if($this->user_model == 4){
                $list = M("Logistics")->select();
            }else{
                $list[] = get_logistics_data();
            }

            $this->assign("list",$list);
            $this->display();
        }

        /**
         * 物流公司充值统计报表
         */
        public function recharge_report(){
            $user_model = $this->user_model;
            $type = I("request.type");  //等于1  日报 等于2  月报
            if(empty($type)){
                $type = 1;
            }
            if($user_model == 4){
                $s_rid = I("POST.s_rid");
                $this->assign("s_rid",$s_rid);

                if(!empty($s_rid)){
                    $where = array(
                        'b.lid'=>$s_rid
                    );
                }else{
                    $where = array();
                }

            }else{
                $where = array(
                    'b.lid'=>get_logistics_id()
                );
            }

            $this->assign("user_model",$user_model);
            $logistics_list = $this->m_logistics->get_list();
            $this->assign("logistics_list",$logistics_list);

            $count= M("LogisticsBill")->alias("b")->where($where)->count();
            $page = $this->page($count, 15);
            $list = M("LogisticsBill")->alias("b")->field("b.*,l.logistics_name")->where($where)
                ->join("bdt_logistics l ON l.id = b.lid")
                ->order("id DESC")
                ->limit($page->firstRow , $page->listRows)
                ->select();
            $result = array();
            if($type == 1){
                foreach($list AS $litem){
                    $key = date("Ymd".$litem['lid'],$litem['create_time']);
                    if(empty($result[$key])){
                        $result[$key]['balance'] = $litem['balance'];
                        $result[$key]['logistics_name'] = $litem['logistics_name'];
                        $result[$key]['time'] = date("Y-m-d",$litem['create_time']);
                        $result[$key][$litem['model']] = $litem['price'];
                    }else{
                        $result[$key][$litem['model']] = $result[$key][$litem['model']]+$litem['price'];
                    }

                }
            }else{
                foreach($list AS $litem){
                    $key = date("Ym".$litem['lid'],$litem['create_time']);
                    if(empty($result[$key])){
                        $result[$key]['balance'] = $litem['balance'];
                        $result[$key]['logistics_name'] = $litem['logistics_name'];
                        $result[$key]['time'] = date("Y年m月",$litem['create_time']);
                        $result[$key][$litem['model']] = $litem['price'];
                    }else{
                        $result[$key][$litem['model']] = $result[$key][$litem['model']]+$litem['price'];
                    }

                }
            }
            $this->assign("type",$type);
            $this->assign("list",$result);
            $this->display();
        }

        /**
         * 机构发卡统计日报
         */
        public function card_report(){

            if($this->user_model == 4){
                $where = array();
            }else{
                $where['id'] = get_logistics_id();
            }
            $list = M("Logistics")->where($where)->select();
            $this->assign("list",$list);
            $this->display();
        }

        /**
         * 物流公司统计报表
         */
//        public function index(){
//            $count=$this->m_driver->count();
//            $page = $this->page($count, 15);
//            $list = $this->m_driver->field("d.*,l.logistics_name")->alias("d")
//                ->join("bdt_logistics l ON l.id = d.lid")
//                ->order("create_time desc")
//                ->limit($page->firstRow , $page->listRows)
//                ->select();
//            $this->assign("page", $page->show('Admin'));
//            $this->assign("list",$list);
//            $this->display();
//        }

    }